package starwars.example;

import java.io.IOException;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.SingleSceneSplitScreenEngine;
import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.util.FPSLogger;
import android.util.Log;
import com.badlogic.gdx.math.Vector2;



public class StarWarsActivity extends BaseExample {

	private static int CAMERA_WIDTH = 400;
	private static int CAMERA_HEIGHT = 400;

	private Camera mCamera;
	private Camera mChaseCamera;


	Vector2 acceleration = new Vector2(0,0);
	Vector2 velocity = new Vector2(0,0);
	
	FixtureAtlas fAtlas=new FixtureAtlas();
	
	public Engine onLoadEngine() {
		//CAMERA_WIDTH = display.getWidth();
		//CAMERA_HEIGHT = display.getHeight()*2;
	 
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		this.mChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, 0, CAMERA_WIDTH, 0, CAMERA_HEIGHT);
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH * 2, CAMERA_HEIGHT), this.mCamera);
		engineOptions.getTouchOptions().setRunOnUpdateThread(true);
		return new SingleSceneSplitScreenEngine(engineOptions, this.mChaseCamera);
				
	}

	
	public void onLoadResources() {
		try {
			fAtlas.loadFromJSON(mEngine,this,getAssets().open("starwars.json"));
		} catch (IOException e) {
			Log.d("JSON",e.getMessage());
		}
	}

	private Scene scene;
	private Field field;
	
	public Scene onLoadScene() {
		mEngine.registerUpdateHandler(new FPSLogger());
		scene = new Scene();
		field = new Field(new Vector2(0, 0), false);
		
		field.attachScene(scene, CAMERA_WIDTH, CAMERA_HEIGHT);
		
		scene.registerUpdateHandler(field);
		
		final Item spaceship = new Spaceship(field,fAtlas,"spaceship",200,300);
		
		for(int i=0;i<10;i++){
			new Tracer(field,fAtlas,"tracer",i*35,i*35);
		}
		
		

		
		mChaseCamera.setChaseEntity(spaceship.sprite);
		
		IAnalogOnScreenControlListener controlListener=new IAnalogOnScreenControlListener() {
			public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
				acceleration.set(pValueX , pValueY);
				velocity = spaceship.getLinearVelocity();

				if((acceleration.len() > 0.1f)&&(velocity.len()<10)){
					velocity.add(acceleration);
					spaceship.setLinearVelocity(velocity);

					final float rotationInRad = (float)Math.atan2(-pValueX, pValueY)-90;
					spaceship.setTransform(spaceship.getWorldCenter(), rotationInRad);
					//spaceship.setRotation(rotationInRad);
					spaceship.setLinearDamping(1);
					spaceship.setAngularDamping(1);

				}
				
			}
			public void onControlClick(AnalogOnScreenControl arg0) {}
		};

	
		new OnScreenControl(scene,mEngine,this,mCamera,CAMERA_WIDTH,CAMERA_HEIGHT,controlListener);

		return scene;
	}
	
	public void onLoadComplete() {}

	public void onResumeGame() {
		super.onResumeGame();
	}
	
	public void onPauseGame() {
		super.onPauseGame();
	}
}
